package com.shanxi.cz.module.biz.service.ammeterbasicinfo;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.shanxi.cz.module.biz.controller.admin.ammeterbasicinfo.vo.*;
import com.shanxi.cz.module.biz.dal.dataobject.ammeterbasicinfo.AmmeterBasicInfoDO;
import com.shanxi.cz.framework.common.pojo.PageResult;
import com.shanxi.cz.framework.common.util.object.BeanUtils;

import com.shanxi.cz.module.biz.dal.mysql.ammeterbasicinfo.AmmeterBasicInfoMapper;

import static com.shanxi.cz.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.shanxi.cz.framework.common.util.collection.CollectionUtils.convertList;
import static com.shanxi.cz.module.biz.enums.ErrorCodeConstants.*;

/**
 * 电表基础信息 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class AmmeterBasicInfoServiceImpl implements AmmeterBasicInfoService {

    @Resource
    private AmmeterBasicInfoMapper ammeterBasicInfoMapper;

    @Override
    public Integer createAmmeterBasicInfo(AmmeterBasicInfoSaveReqVO createReqVO) {
        // 插入
        AmmeterBasicInfoDO ammeterBasicInfo = BeanUtils.toBean(createReqVO, AmmeterBasicInfoDO.class);
        ammeterBasicInfoMapper.insert(ammeterBasicInfo);

        // 返回
        return ammeterBasicInfo.getId();
    }

    @Override
    public void createAmmeterBasicInfoList(List<AmmeterBasicInfoSaveReqVO> ammeterBasicInfoList) {
        // 插入
        List<AmmeterBasicInfoDO> ammeterBasicInfoDOList = BeanUtils.toBean(ammeterBasicInfoList, AmmeterBasicInfoDO.class);
        ammeterBasicInfoMapper.insertBatch(ammeterBasicInfoDOList);
    }

    @Override
    public void updateAmmeterBasicInfo(AmmeterBasicInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateAmmeterBasicInfoExists(updateReqVO.getId());
        // 更新
        AmmeterBasicInfoDO updateObj = BeanUtils.toBean(updateReqVO, AmmeterBasicInfoDO.class);
        ammeterBasicInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteAmmeterBasicInfo(Integer id) {
        // 校验存在
        validateAmmeterBasicInfoExists(id);
        // 删除
        ammeterBasicInfoMapper.deleteById(id);
    }

    @Override
        public void deleteAmmeterBasicInfoListByIds(List<Integer> ids) {
        // 删除
        ammeterBasicInfoMapper.deleteByIds(ids);
        }


    private void validateAmmeterBasicInfoExists(Integer id) {
        if (ammeterBasicInfoMapper.selectById(id) == null) {
            throw exception(AMMETER_BASIC_INFO_NOT_EXISTS);
        }
    }

    @Override
    public AmmeterBasicInfoDO getAmmeterBasicInfo(Integer id) {
        return ammeterBasicInfoMapper.selectById(id);
    }

    @Override
    public List<AmmeterBasicInfoDO> getAmmeterBasicInfoAll() {
        return ammeterBasicInfoMapper.selectList();
    }

    @Override
    public PageResult<AmmeterBasicInfoDO> getAmmeterBasicInfoPage(AmmeterBasicInfoPageReqVO pageReqVO) {
        return ammeterBasicInfoMapper.selectPage(pageReqVO);
    }

}